package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.domain.Order;
import com.itheima.entity.Result;
import com.itheima.service.OrderServie;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/orderController")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderServie orderServie;

    //发送体检预约验证码
    @RequestMapping("sendCode4Order")
    public Result send4Order(String phoneNumber){
        //生成随机六位验证码
        Integer integer = ValidateCodeUtils.generateValidateCode(4);

        try {
            //向用户发送验证码
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phoneNumber,integer.toString());
            //将验证码存入redis
            jedisPool.getResource().setex(phoneNumber+ RedisMessageConstant.SENDTYPE_ORDER,600,integer.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    /**
     * 提交预约信息
     * @param map
     * @return
     */
    @RequestMapping("submit")
    public Result submit(@RequestBody Map map){
        //1.校验验证码输入是否正确
        //获取请求参数验证码和手机号码
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        //redis中的验证码
        String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if (code != null && code.equals(validateCode)  ) {
            //2.验证码输入正确,调用服务,完成预约处理
            map.put("orderType", Order.ORDERTYPE_WEIXIN);//设置预约类型，分为微信预约、电话预约
            //远程调用服务
            Result result = null;
            try {
                result = orderServie.order(map);
            } catch (Exception e) {
                e.printStackTrace();
                return result;
            }

            if (result.isFlag()) {
                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone, (String) map.get("orderDate"));
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }else {
            //如果验证码比对不成功，返回结果给页面
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

    }

    /**
     * 查询预约详细信息
     * @param id
     * @return
     */
    @RequestMapping("findOrderById")
    public Result findById(Integer id){
        try {
            Map map = orderServie.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //发送快速登录验证码
    @RequestMapping("send4Login")
    public Result sendCode4Login(String telephone){
        //生成随机六位验证码
        Integer integer = ValidateCodeUtils.generateValidateCode(6);
        System.out.println("phoneNumber = " + telephone);
        try {
            //向用户发送验证码
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,integer.toString());
            //将验证码存入redis
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,300,integer.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }


}
